<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Think\Controller;
/**
 * 后台提现记录控制器
 * @author 
 */
class WithdrawalsController extends AdminController {
    /**
     * 提现记录
     */
    public function index(){
    	//搜索
    	$keyword = I('keyword', '', 'string');
        $condition = array('like','%'.$keyword.'%');
        $map_user['id|username|email|mobile'] = array($condition, $condition, $condition, $condition,'_multi'=>true);
        $userids=M('User')->where($map_user)->getField('id',true);
        if(!empty($userids)){
        	$map['userid']=array('in',$userids);
        }else{
        	$map['userid']=0;
        }
        
    	//设置Tab导航数据列表
    	//获取可提现币种
    	$can_money_type_list=M('MoneyType')->where(array('status'=>1,'is_withdrawals'=>1))->select();
    	if(empty($can_money_type_list)){
    		$this->error('没有可提现币种!');
    	}
    	//默认币种
    	$money_type_default=$can_money_type_list[0]['identification'];
    	$can_money_type_arr=list_to_arr($can_money_type_list, 'identification', 'name');
    	foreach($can_money_type_arr as $key => $val){
    		$tab_list[$key]['title'] = $val;
    		$tab_list[$key]['href']  = U('index', array('group' => $key));
    	}
    	$group=I('group',$money_type_default);
    	$map['money_type']=$group;
    	
    	//设置下拉搜索项
    	//根据数据词典表获取提现状态
    	$with_status_list=M('Descriptions')->where(array('tablename'=>'withdrawals_status'))->select();
    	$keyselect=I('keyselect');
    	if(!empty($keyselect)){
    		$map['process_status']=$keyselect;
    	}
    	
    	//预定义按钮属性以简化使用
    	$my_attribute['title'] = '详情';
    	$my_attribute['class'] = 'label label-xq';
    	$my_attribute['href'] ='javascript:void(0);';
    	$my_attribute['onclick']='show_with_info(__data_id__)';
    	
    	//iframe层
    	$extra_html='<script type="text/javascript" src="./Public/extra/js/with_info.js" charset="utf-8"></script>';
    	
    	$attr['field']='process_status';
    	$attr[1]['title'] = '批准';
    	$attr[1]['class'] = 'label label-success ajax-get confirm';
    	$attr[1]['href']  = U('Withdrawals/review', array('id' => '__data_id__','with_type'=>1));
    	
    	//预定义按钮属性以简化使用
    	$attr1['field']='process_status';
    	$attr1[1]['title'] = '拒绝';
    	$attr1[1]['class'] = 'label label-warning';
    	$attr1[1]['href']  = 'javascript:void(0);';
    	$attr1[1]['onclick']  = 'recharge_review_refuse(__data_id__)';
    	
    	$data_list = D('Withdrawals')->page(!empty($_GET["p"])?$_GET["p"]:1, C('ADMIN_PAGE_ROWS'))->where($map)->order('id desc')->select();
    	$page = new \Common\Util\Page(D('Withdrawals')->where($map)->count(), C('ADMIN_PAGE_ROWS'));
    	//使用Builder快速建立列表页面。
        $builder = new \Common\Builder\ListBuilder();
        $builder->setMetaTitle('提现列表') //设置页面标题
        		//->addTopButton('delete')  //添加删除按钮
        		->setSearch('请输入用户ID/用户名/邮箱/手机号', U('index', array('group' => $group)),$with_status_list)
        		->SetTabNav($tab_list, $group) //设置Tab按钮列表
        		
                ->addTableColumn('id', 'ID')
                ->addTableColumn('userid', '用户','function','get_username')
                ->addTableColumn('amount', '提现金额')
                ->addTableColumn('fee', '手续费')
                ->addTableColumn('fack_receive', '到账金额')
                ->addTableColumn('process_status', '状态', 'function','get_with_status_text')
                ->addTableColumn('withdraw_date', '申请时间', 'time')
                ->addTableColumn('right_button', '操作', 'btn')
                ->setTableDataList($data_list) //数据列表
                ->setTableDataPage($page->show()) //数据列表分页
                ->addRightButton('self',$my_attribute)   //显示详情
				->addRightButton('caseButton','',$attr)//批准
				->addRightButton('caseButton','',$attr1)//拒绝
                ->addRightButton('delete') //添加删除按钮
                ->setExtraHtml($extra_html)
                ->display();
    }
    /**
     * 提现详情
     */
    public function info(){
    	$id=I('id');
    	$with_info=M('Withdrawals')->where(array('id'=>$id))->find();
    	$data.="开户名:".$with_info['name']."<br/>";
    	$data.="开户行:".$with_info['bank']."<br/>";
    	$data.="银行账号:".$with_info['bank_no']."<br/>";
    	print_r($data); 
    	exit;
    }
    /**
     * 提现审核
     */
    public function review(){
    	$with_type=I('with_type');
    	$with_id=I('id');
    
    	$with_info=M('Withdrawals')->where(array('id'=>$with_id,'process_status'=>1))->find();
    	if(empty($with_info)){
    		$this->error('该信息不存在!');
    	}
    	if($with_type==1){//批准
    		M('Withdrawals')->where(array('id'=>$with_id))->setField(array('process_status'=>2,'confirm_date'=>time()));
    		//提现人id
    		$userid=$with_info['userid'];
    		$log_data['title']=$message_data['title']="批准提现申请";
    		$log_data['to_uid']=$message_data['to_uid']=$userid;
    		$log_data['content']="批准".get_username($userid)."的提现申请，申请ID:".$with_info['id'];
    		$message_data['content']="批准了".get_username($userid)."的提现申请，快去查看吧！";
    		add_log_message($log_data,$message_data);
    		$this->success('操作成功!');
    	}elseif($with_type==-1){//拒绝
    		$remark=I('remark');
    		M('Withdrawals')->where(array('id'=>$with_id))->setField(array('process_status'=>-1,'remark'=>$remark,'confirm_date'=>time()));
    		//提现币种
    		$money_type=$with_info['money_type'];
    		//提现金额
    		$money=$with_info['amount'];
    		if(!($money>0)){
				$return['status']=false;
				$return['msg']='提现金额不正确';
				$this->ajaxReturn($return);
			}
			//提现人id
			$userid=$with_info['userid'];

			
    		$moneychange_model=D('Detailed'.ucfirst($money_type));
			$changedate['type']=7;//流水类型
			$changedate['money']=$money;//操作金额
			$changedate['userid']=$userid;//对应用户
			$changedate['relevant_userid']=1;//起因用户
			$changedate['remark']="拒绝提现返还";//详细信息
			$res=$moneychange_model->changemoney($changedate);//财务流水记录
    		
    		if($res){
    			$log_data['title']=$message_data['title']="拒绝提现申请";
    			$log_data['to_uid']=$message_data['to_uid']=$userid;
    			$log_data['content']="拒绝".get_username($userid)."的提现申请，申请ID:".$with_info['id'];
    			$message_data['content']="拒绝了".get_username($userid)."的提现申请，快去查看吧！";
    			add_log_message($log_data,$message_data);
    			$return['status']=1;
				$return['msg']='操作成功';
				$this->ajaxReturn($return);
    		}else{
    			$return['status']=false;
				$return['msg']='操作失败';
				$this->ajaxReturn($return);
    		}
    	}
    }
}
